package 除法;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LoopFestival循环节 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // String str = scan.next();     // 0.[846153]
        // int x = Integer.parseInt(str.split(",")[0]);
        // int y = Integer.parseInt(str.split(",")[1]);
        // function(x,y);
        System.out.println((double) 1/7);
        loopFestival(1,8);
        System.out.println();
        for (int i = 1; i < 100; i++) {
            System.out.print(i+"\t");
            loopFestivalOptimization(1,i);
            System.out.println();
        }

    }

    public static void loopFestival(int x,int y){
        int remainder = 0; // 余数
        List<Integer> number = new ArrayList<>();
        number.add(x/y);
        remainder = x%y;
        for (int  numberOfDigits = 1; numberOfDigits <= 100000; numberOfDigits++) {
            number.add((remainder*10)/y);
            remainder = (remainder*10)%y;
            if(remainder == 0){
                System.out.print(number.get(0)+".");
                for (int j = 1; j <= numberOfDigits; j++) {
                    System.out.print(number.get(j));
                }
                break;
            }else if(numberOfDigits % 2 == 0) {
                boolean flag = true;
                for (int i = 1; i <= numberOfDigits/2; i++) {
                    if(number.get(i) != number.get(i+numberOfDigits/2))
                        flag = false;
                }
                if(flag == true){
                    System.out.print(number.get(0)+".[");
                    for (int k = 1; k <= numberOfDigits/2; k++) {
                        System.out.print(number.get(k));
                    }
                    System.out.print("]");
                    break;
                }
            }
        }
    }
    public static void loopFestivalOptimization(int x,int y){
        int remainder = 0; // 余数
        List<Integer> number = new ArrayList<>();
        number.add(x/y);
        remainder = x%y;
        for (int  numberOfDigits = 1; numberOfDigits <= 100000; numberOfDigits++) {
            // System.out.println("numberOfDigits:"+numberOfDigits);
            number.add((remainder*10)/y);
            remainder = (remainder*10)%y;
            if(remainder == 0){
                System.out.print(number.get(0)+".");
                for (int j = 1; j <= numberOfDigits; j++) {
                    System.out.print(number.get(j));
                }
                break;
            }else if(numberOfDigits % 2 == 0) {
                boolean flag = true;
                for (int i = 1; i <= numberOfDigits/2; i++) {
                    if(number.get(i) != number.get(i+numberOfDigits/2)){
                        flag = false;
                        break;
                    }
                }
                if(flag == true){
                    System.out.print(number.get(0)+".[");
                    for (int k = 1; k <= numberOfDigits/2; k++) {
                        System.out.print(number.get(k));
                    }
                    System.out.print("]");
                    break;
                }
            }
        }
    }
}
